package com.video.controller.webcon;

import com.video.commons.loginaop.LoginContact;
import com.video.commons.loginaop.NoMustLogin;
import com.video.entity.Webcon;
import com.video.service.webcon.IWebconService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * 分控网页请求控制器
 */
@Controller
@RequestMapping("/webcon")
public class WebController {

    @Autowired
    private IWebconService webconService;

    /**
     * 进入分控的登录页面
     * @return
     */
    @NoMustLogin
    @RequestMapping("")
    public String welcome(){
        return "webcon/login";
    }

    /**
     * 分控登录
     * @return
     */
    @NoMustLogin
    @RequestMapping("/login")
    public String login(String account, String password, HttpSession session, Model model){

        //分控登录
        Webcon webcon = webconService.getById(account);
        if (webcon != null){
            if (webcon.getPassword().equals(password)){
                //保存进session中
                session.setAttribute(LoginContact.WEBCON_USER.getKey(), webcon);
//                model.addAttribute("golang", golangServer);
                //登录成功
                return "webcon/index";
            } else {
                //密码错误
                model.addAttribute("error", "密码错误！");
            }
        } else {
            //账号不存在
            model.addAttribute("error", "分控账号不存在！");
        }
        return "webcon/login";
    }

    /**
     * 注销
     * @return
     */
    @NoMustLogin
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute(LoginContact.WEBCON_USER.getKey());
        return "webcon/login";
    }
}
